﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using Model.TempClass;

namespace DAL
{
    public class SysProcessDAL
    {
        /// <summary>
        /// 添加节点
        /// </summary>
        /// <param name="Flow"></param>
        /// <returns></returns>
        public int Add(FlowModel Flow)
        {
            using (FlowDBEntities db = new FlowDBEntities())
            {
                SysProcess proModel = new SysProcess();
                proModel.flow_id = int.Parse(Flow.flow_id);
                proModel.icon = Flow.icon;
                proModel.process_name = Flow.process_name;
                proModel.style = Flow.style;
                db.SysProcess.Add(proModel);
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 修改样式
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int EditStyle(SysProcess entity) {
            try
            {
                using (FlowDBEntities db=new FlowDBEntities())
                {
                    SysProcess model= db.SysProcess.Where(i => i.id == entity.id).FirstOrDefault();
                    if (model!=null) {
                        model.style = entity.style;
                        model.UpdateTime = DateTime.Now;
                    }
                    return db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
        /// <summary>
        /// 保存详情
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int SaveDetail(SysProcess entity)
        {
            using (FlowDBEntities db = new FlowDBEntities())
            {
                SysProcess model = db.SysProcess.Where(i => i.id == entity.id).FirstOrDefault();
                if (model != null)
                {
                    model.OperationID = entity.OperationID;
                    model.Other = entity.Other;
                    model.process_name = entity.process_name;
                    model.RoleID = entity.RoleID;
                    model.SuggesItem = entity.SuggesItem;
                    model.SuggesLabel = entity.SuggesLabel;
                    model.Approval = entity.Approval;
                    model.Atten = entity.Atten;
                    model.BackID = entity.BackID;
                    model.Buttons = entity.Buttons;
                    model.Condition = entity.Condition;
                    model.FunctionType = entity.FunctionType;
                    model.icon = entity.icon;
                    model.UpdateTime = DateTime.Now;
                }
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 根据id获取节点详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public SysProcess GetDateil(int id)
        {
            using (FlowDBEntities db = new FlowDBEntities())
            {
                SysProcess model = new SysProcess();
                model = db.SysProcess.Where(i => i.id == id).FirstOrDefault();

                return model;

            }
        }
        /// <summary>
        /// 获取OA审批角色信息
        /// </summary>
        /// <param name="flowid"></param>
        /// <returns></returns>
        public List<SelectModel> GetRole(string flowid)
        {
            List<SelectModel> list = new List<SelectModel>();
            using (CFA_OAEntities dbs = new CFA_OAEntities())
            {
                list = dbs.SysFlowRole.AsQueryable().Where(i => i.FlowCode == flowid).Select(i => new SelectModel { Id = i.ID.ToString(), Text = i.Name }).ToList();
                return list;
            }
        }
        /// <summary>
        /// 获取操作事件
        /// </summary>
        /// <returns></returns>
        public List<SelectModel> GetOperation()
        {
            List<SelectModel> list = new List<SelectModel>();
            using (CFA_OAEntities dbs = new CFA_OAEntities())
            {
                list = dbs.SysFlowOperation.AsQueryable().Where(i => true).Select(i => new SelectModel { Id = i.ID.ToString(), Text = i.Operation }).ToList();
                return list;
            }
        }
        /// <summary>
        /// 获取操作权限
        /// </summary>
        /// <returns></returns>
        public List<SelectModel> GetBtn()
        {
            List<SelectModel> list = null;
            using (CFA_OAEntities dbs = new CFA_OAEntities())
            {
                list = dbs.SysFlowButton.AsQueryable().Where(i => true).Select(i => new SelectModel { Id = i.ID.ToString(), Text = i.Name }).ToList();
                return list;

            }
        }
        /// <summary>
        /// 获取审批意见
        /// </summary>
        /// <returns></returns>
        public List<SelectModel> GetItem()
        {
            List<SelectModel> list = null;
            using (CFA_OAEntities dbs = new CFA_OAEntities())
            {
                list = dbs.SysFlowSuggestion.AsQueryable().Where(i => true).Select(i => new SelectModel { Id = i.ID.ToString(), Text = i.Name }).ToList();
                return list;

            }
        }
        /// <summary>
        /// 获取可退回的步骤
        /// </summary>
        /// <returns></returns>
        public List<SelectModel> GetBackProcess(int flowID)
        {
            List<SelectModel> list = null;
            using (FlowDBEntities dbs = new FlowDBEntities())
            {
                list = dbs.SysProcess.AsQueryable().Where(i => i.flow_id== flowID).Select(i => new SelectModel { Id = i.id.ToString(), Text = i.process_name }).ToList();
                return list;

            }
        }
        /// <summary>
        /// 获取当前流程所有步骤
        /// </summary>
        /// <param name="flow"></param>
        /// <returns></returns>
        public List<SysProcess> GetList(int flow)
        {
            using (FlowDBEntities db = new FlowDBEntities())
            {
                List<SysProcess> listPro = db.SysProcess.Where(i => i.flow_id == flow).Select(i => i).ToList();
                return listPro;
            }
        }
        /// <summary>
        /// 获取当前步骤和下一步骤的关系
        /// </summary>
        /// <param name="process_id"></param>
        /// <returns></returns>
        public List<SysRelation> GetRelation(int process_id) {
            using (FlowDBEntities db = new FlowDBEntities())
            {
                List<SysRelation> listRelation = db.SysRelation.Where(i => i.process_id == process_id).Select(i => i).ToList();
                return listRelation;
            }
        }
        /// <summary>
        /// 添加关系
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int AddRelation(SysRelation entity) {
            using (FlowDBEntities db = new FlowDBEntities())
            {
                db.SysRelation.Add(entity);
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 根据步骤id，删除有关的关系数据
        /// </summary>
        /// <param name="process_id"></param>
        /// <returns></returns>
        public int DeleteRelationAll(int process_id) {
            using (FlowDBEntities db = new FlowDBEntities())
            {
                List<SysRelation> listRelation = db.SysRelation.Where(i => i.process_id == process_id).Select(i => i).ToList();

                if (listRelation != null && listRelation.Count > 0)
                {
                    db.SysRelation.RemoveRange(listRelation);
                }
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 根据id删除步骤
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteProcess(int id) {
            using (FlowDBEntities db = new FlowDBEntities())
            {
                SysProcess model = db.SysProcess.Where(item => item.id == id).FirstOrDefault();
                if (model != null)
                {
                    db.SysProcess.Remove(model);
                }
                return db.SaveChanges();

            }
        }
        /// <summary>
        /// 获取当前流程的全部关系数据
        /// </summary>
        /// <param name="flowid"></param>
        /// <returns></returns>
        public List<SysRelation> GetRelationAll(int flowid){
            using (FlowDBEntities db=new FlowDBEntities())
            {
                List<SysRelation> list = db.SysRelation.Where(i=>i.flowID==flowid).Select(item => item).ToList();
                return list;
            }
        }
    }
}
